package com.ciaojian.worker.controller;


import com.ciaojian.core.enums.business.WorkerTypeEnum;
import com.ciaojian.core.mapper.OrderMapper;
import com.ciaojian.core.mapper.WorkerTurnoverMapper;
import com.ciaojian.core.model.Worker;
import com.ciaojian.core.util.Result;
import com.ciaojian.worker.service.*;
import io.swagger.annotations.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * 业绩Controller
 *
 * @author Atlakyn
 */
@Api(tags = "业绩接口")
@RestController
@RequestMapping("/performance")
public class PerformanceController {
    @Resource
    private PerformanceServiceImpl performanceService;
    @Resource
    private WorkerServiceImpl workerService;
    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private MemberOrderServiceImpl memberOrderService;
    @Resource
    private WorkerTurnoverMapper workerTurnoverMapper;

    @ApiOperation("会员销售数据")
    @GetMapping("/member-sales")
    public Result getMemberSales(String month, Integer workerId) {
        return Result.ok(orderService.getBaseMapper().sumMemberSales(month, workerId));
    }

    @ApiOperation("获取业绩数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "月份", paramType = "query", example = "2021-07"),
            @ApiImplicitParam(name = "workerId", value = "发型师ID", paramType = "query")
    })
    @GetMapping("/data")
    public Result getData(String month, Integer workerId) {
        return Result.ok(workerTurnoverMapper.findByMonthAndStore(month, workerId));
    }

    /**
     * @Description: 删除发型师作品
     * @Author: xiangchao
     * @Date: 2020/12/1 12:52 下午
     */
    @ApiOperation("删除发型师作品")
    @GetMapping("delWorkerPhoto")
    public Result delWorkerPhoto(@ApiParam(value = "作品ID", required = true) String ids) {
        return workerService.delWorkerPhoto(ids);
    }

    /**
     * @Description: 查询班次
     * @Author: xiangchao
     * @Date: 2020/11/30 10:00 上午
     */
    @ApiOperation("查询班次")
    @GetMapping("findDRosterList")
    public Result findDRosterList(@ApiParam(value = "门店ID", required = true) String storeId) {
        return workerService.findDRosterList(storeId);
    }

    /**
     * 1
     *
     * @param storeId
     * @param workerId
     * @param startDate
     * @param endDate
     * @return
     */
    @ApiOperation("获取门店和发型师业绩")
    @GetMapping("/{storeId}/{workerId}/{startDate}/{endDate}")
    public Result getPerformance(@PathVariable Integer storeId,
                                 @PathVariable Integer workerId,
                                 @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                                 @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate) {
        return Result.ok(performanceService.getPerformance(storeId, workerId, startDate, endDate));
    }

    @ApiOperation("获取个人数据")
    @Deprecated
    @GetMapping("/personal-data/{workerId}/{startDate}/{endDate}")
    public Result getPersonalData(@PathVariable Integer workerId,
                                  @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                                  @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate
    ) {
        Assert.isTrue(startDate.before(endDate), "请输入正确的日期");
        return Result.ok(workerService.findPersonalData(workerId, startDate, endDate));
    }

    @ApiOperation("门店发型师业绩数据列表")
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/store-worker/data/{type}/{storeId}/{startDate}/{endDate}")
    public Result listByStore(@PathVariable int type,   //0 员工在该店铺真实业绩  //1 员工总业绩
                              @PathVariable Integer storeId,
                              @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                              @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        List<Worker> workerList = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<>();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endDate);
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        calendar.add(Calendar.MINUTE, -1);
        endDate = calendar.getTime();
        Date finalEndDate = endDate;

        if (type == 1) {
            workerList = workerService.listByStoreId(storeId);
            storeId = null;
        } else {
            workerList = orderService.listByStoreId(storeId, startDate, finalEndDate);
        }

        Integer finalStoreId = storeId;

        if (workerList.size()==0){
            return Result.ok(workerList);
        }

        workerList.forEach(worker -> {
            Map<String, Object> map = workerService.findPersonalDataV2(finalStoreId, worker.getId(), startDate, finalEndDate);

            list.add(map);
        });


        return Result.ok(list);
    }

    /**
     * 2
     *
     * @param workerId
     * @param startDate
     * @param endDate
     * @return
     */
    @ApiOperation("发型师业绩数据")
    @GetMapping("/personal-data/v2/{workerId}/{startDate}/{endDate}")
    public Result getPersonalDataV2(@PathVariable Integer workerId,
                                    @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                                    @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate
    ) {
        Assert.isTrue(startDate.before(endDate), "请输入正确的日期");
        return Result.ok(workerService.findPersonalDataV2(null, workerId, startDate, endDate));
    }

    @ApiOperation("我的订单")
    @GetMapping("/order/{startDate}/{endDate}/{workerId}")
    public Result getOrder(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                           @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
                           @PathVariable Integer workerId,
                           @RequestParam(required = false) Integer orderType,
                           @RequestParam(defaultValue = "1") Integer pageNo,
                           @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        Assert.isTrue(startDate.before(endDate), "请输入正确的日期");
        return Result.ok(orderService.getMyOrder(startDate, endDate, workerId, orderType, pageNo, pageSize));
    }

    @ApiOperation("发型师数据")
    @GetMapping("/worker-data/{storeId}/{userId}/{startDate}/{endDate}")
    public Result getWorkerData(@PathVariable Integer storeId,
                                @PathVariable Integer userId,
                                @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                                @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate,
                                @RequestParam(defaultValue = "1") Integer pageNo,
                                @RequestParam(defaultValue = "10") Integer pageSize) {
        WorkerTypeEnum type = new Worker().selectById(userId).getType();
        Assert.isTrue(type.equals(WorkerTypeEnum.landlord), "没有访问权限");

        return workerService.getWorkerData(storeId, startDate, endDate, pageNo, pageSize);
    }

    @ApiOperation("查询发型师邀请的会员订单接口")
    @GetMapping("/member-order/{inviteId}/{startTime}/{endTime}")
    public Result getMemberOrder(@PathVariable Integer inviteId,
                                 @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                 @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                 @RequestParam(defaultValue = "1") Integer pageNo,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.ok(memberOrderService.findMyInvitedOrder(inviteId, startTime, endTime, pageNo, pageSize));
    }


    @ApiOperation("查询店铺数据")
    @GetMapping("/store-data/{storeId}")
    public Result getStorePerformance(@PathVariable Integer storeId,
                                      @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                      @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        if (startTime.equals(endTime)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(endTime);
            calendar.add(Calendar.DAY_OF_MONTH, +1);
            calendar.add(Calendar.MINUTE, -1);
            endTime = calendar.getTime();
        }
        return orderService.getStorePerformance(storeId, startTime, endTime);
    }

    @ApiOperation("业绩同比")
    @GetMapping("/store-monthGap/{storeId}/{startDate}")
    public Result storeMonthGap(@PathVariable Integer storeId,
                                      @PathVariable  String startDate) throws ParseException {

        return orderService.getStoreMonthGap(storeId, startDate);
    }
}

